package com.geeksaga.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.geeksaga.dao.BbsDao;
import com.geeksaga.dao.BbsStarDao;
import com.geeksaga.domain.Bbs;
import com.geeksaga.domain.BbsStar;

/**
 * @author newbie
 * @since 0.1
 */
@Service
public class BbsStarServiceImpl implements BbsStarService {
    
    @Autowired protected BbsDao bbsDao;
    @Autowired protected BbsStarDao bbsStarDao;
    
    /* (non-Javadoc)
     * @see com.geeksaga.service.BbsStarService#addAfterCalculate(com.geeksaga.domain.BbsStar)
     */
    public BbsStar addAfterCalculate(BbsStar bbsStar) throws DataAccessException {
        BbsStar star = null;
        
        if(!bbsStarDao.exists(bbsStar)) {
            bbsStarDao.add(bbsStar);
            
            star = bbsStarDao.calculate(bbsStar);
            
            Bbs bbs = new Bbs();
            bbs.setSid(bbsStar.getBbsSid());
            bbs.setStarCnt(star.getCount());
            bbs.setStarSum(star.getSum());
            
            bbsDao.updateCnt(bbs, Bbs.CNT_TYPE.STAR_PLUS_CNT);
        } else
            star = bbsStarDao.calculate(bbsStar);
        
        return star;
    }
}